package com.ifp.core.connector;// package com.ifp.opengate.boot.connector;

//
// import com.ifp.adapter.common.AdapterCanstants;
// import com.ifp.core.data.DataElement;
// import com.ifp.core.log.Trace;
// import com.ifp.core.util.DateUtil;
// import com.ifp.core.util.SpringContextsUtil;
// import com.ifp.gateway.bean.MessageDefine;
// import com.ifp.gateway.bean.MsgField;
// import com.ifp.gateway.formatter.IFormatter;
// import net.sf.json.JSONObject;
// import org.apache.commons.collections.map.HashedMap;
// import org.apache.commons.httpclient.HttpClient;
// import org.apache.commons.httpclient.methods.PostMethod;
// import org.apache.commons.httpclient.methods.RequestEntity;
// import org.apache.commons.httpclient.methods.StringRequestEntity;
// import org.apache.commons.io.IOUtils;
// import org.apache.commons.lang3.StringUtils;
//
// import java.io.InputStream;
// import java.net.InetAddress;
// import java.net.UnknownHostException;
// import java.security.SecureRandom;
// import java.util.Date;
// import java.util.HashMap;
// import java.util.Map;
//
/// **
// * @className: EsbTcpServiceConnector
// * @description:
// * @author: 码农1号
// * @email: guanhh1@belink.com
// * @create: 2019-07-09 18:19
// **/
// public class EsbTcpServiceConnector extends AbstractConnector {
//
// private static final String[] REQ_BODYS = {"userId","userCertLevel","behavCode","pageSize","offset"};
//
// private void handleCommonHead(Map<String, Object> reqHeader,EsbEnum esbEnum) throws UnknownHostException {
// Date date = new Date();
// reqHeader.put(Constants.REQTIME, DateUtil.formatDate(date, "yyyyMMddmmHHss"));
// reqHeader.put(Constants.REQUESTTIME, DateUtil.formatDate(date, "yyyy-MM-dd HH.mm.ss.SSS"));
// reqHeader.put(Constants.APPID, esbEnum.getCode());
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "this SNOWFLAKE is {}", SNOWFLAKE.nextId());
// String snowStr = String.valueOf(SNOWFLAKE.nextId());
// reqHeader.put(Constants.TRANSID, Constants.TRANSID_VALUE+DateUtil.formatDate(date, "yyyyMMdd")+"0000000000"+new
// SecureRandom().ints(0, 9).limit(5).collect(StringBuilder:: new, (sb, str) -> sb.append(str), (sb1, sb2) ->
// sb1.append(sb2)));
// reqHeader.put(Constants.HOSTNAME,esbEnum.getDesc());
// reqHeader.put(Constants.BZTRANSID, Constants.BZTRANSID_VALUE+DateUtil.formatDate(date, "yyyyMMdd")+"0000000"+new
// SecureRandom().ints(0, 9).limit(5).collect(StringBuilder:: new, (sb, str) -> sb.append(str), (sb1, sb2) ->
// sb1.append(sb2)));
// // 新增esb字段 start
// reqHeader.put(Constants.USERRANK, "");
// reqHeader.put(Constants.ORIGINALTRANSID, "");
// reqHeader.put(Constants.CACHEID, "");
// reqHeader.put(Constants.RPTCONTENT, "");
// reqHeader.put(Constants.DATASIGN, Constants.DATASIGN_VALUE);
// reqHeader.put(Constants.SERVICECODE, esbEnum.getServiceId());
// reqHeader.put(Constants.SERVICEADDRESS, esbEnum.getUrl().substring(esbEnum.getUrl().indexOf("webService/")+11));
// reqHeader.put(Constants.CALLBACKURI, "");
// if(EsbEnum.LOATRL0036.getServiceId()==esbEnum.getServiceId()) {
// reqHeader.put(Constants.SRVSYSCD, "039");
// reqHeader.put(Constants.CHNLCD, "0103");
// reqHeader.put(Constants.CLIENTTYP, "200011");
// reqHeader.put(Constants.SCENECD, "");
// reqHeader.put(Constants.CHANNELID, "037");
// reqHeader.put(Constants.SERVICETARGET, "");
// reqHeader.put(Constants.DATASIGN, Constants.DATASIGN036_VALUE);
// reqHeader.put(Constants.SERVICECODE, esbEnum.getServiceId().substring(0, esbEnum.getServiceId().indexOf("|")));
// reqHeader.put("fileName", "");
// reqHeader.put("filePath", "");
// }else if (EsbEnum.LOATRL0035.getServiceId()==esbEnum.getServiceId()) {
// reqHeader.put(Constants.SRVSYSCD, "039");
// reqHeader.put(Constants.CHNLCD, "0103");
// reqHeader.put(Constants.CLIENTTYP, "200011");
// reqHeader.put(Constants.SCENECD, "");
// reqHeader.put(Constants.CHANNELID, "037");
// reqHeader.put(Constants.SERVICETARGET, "");
// reqHeader.put(Constants.SERVICECODE, esbEnum.getServiceId().substring(0, esbEnum.getServiceId().indexOf("|")));
// reqHeader.put(Constants.DATASIGN, Constants.DATASIGN035_VALUE);
// reqHeader.put("fileName", "");
// reqHeader.put("filePath", "");
// }else {
// reqHeader.put(Constants.CHANNELID, "314");
// reqHeader.put(Constants.SERVICETARGET, "ebui");
// }
// reqHeader.put(Constants.REQIP, InetAddress.getLocalHost().getHostAddress());
// // 新增esb字段 end
// // 拼装appsign
// StringBuilder appSignBuilder = new StringBuilder();
// appSignBuilder.append(reqHeader.get(Constants.APPID));
// appSignBuilder.append("_");
// appSignBuilder.append(reqHeader.get(Constants.CHANNELID));
// appSignBuilder.append("_");
// appSignBuilder.append(reqHeader.get(Constants.TRANSID));
// appSignBuilder.append("_");
// appSignBuilder.append(reqHeader.get(Constants.REQTIME));
// String appSign = Md5Utils.getVariantMd5Hex(appSignBuilder.toString());
// reqHeader.put(Constants.APPSIGN, appSign);
// }
//
//
//
// @Override
// public String sendAndReceive(String actionName, String path, Object o) throws Exception {
// Map<String, Object> dataMap = (Map<String, Object>) o;
// Map<String, Object> reqMap = new HashedMap();
// Map<String, Object> reqHeader = new HashedMap();
// Map<String, Object> reqBody = new HashedMap();
// Map<String, Object> headerMap = (Map<String, Object>) dataMap.get(AdapterCanstants.HEADER);
// Map<String, Object> bodyMap = (Map<String, Object>) dataMap.get(AdapterCanstants.BODY);
// String appId = (String) headerMap.get("consumerId");
// EsbEnum esbEnum = EsbEnum.parseCode(appId);
//
// for (String key : REQ_BODYS) {
// if(StringUtils.isNotBlank((String)bodyMap.get(key))) {
// reqHeader.put(key, bodyMap.get(key));
// bodyMap.remove(key);
// }
// }
//
// if(esbEnum ==null) {
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "this serviceId is null");
// }
// String systemId = esbEnum.getSystemId();// 对接系统
// path = esbEnum.getUrl()+"?wsdl";
// handleCommonHead(reqHeader,esbEnum);
// reqMap.put(AdapterCanstants.HEADER, reqHeader);
// // 短信验证两个接口
//
// if("ebui_oam_aun_0015".equals(appId) || "ebui_oam_aun_0022".equals(appId)) {
// Map<String,Object> wrapMap = new HashMap<>();
// wrapMap.put("invoke", bodyMap);
// reqMap.put(AdapterCanstants.BODY, wrapMap);
// }else if("SVR2ECOMMActionWebService|036".equals(appId)){
// Map<String,Object> requestHeaderMap = new HashMap<>();
// requestHeaderMap.put("Data_Typ", "2");
// requestHeaderMap.put("Serv_Typ", "1");
// requestHeaderMap.put("Serv_ID", "SVR_ECOMM_02_0003");
// requestHeaderMap.put("Chnl_Num", "037");
// requestHeaderMap.put("Termn_Num", "06");
// Map<String,Object> reqBusiInfoMap = new HashMap<>();
// Map<String,Object> requestBodyMap = new HashMap<>();
// Map<String,Object> requestMap = new HashMap<>();
// Map<String,Object> executeMap = new HashMap<>();
// reqBusiInfoMap.put("reqBusiInfo", bodyMap);
// requestBodyMap.put("requestBody", reqBusiInfoMap);
// requestBodyMap.put("requestHeader", requestHeaderMap);
// requestMap.put("request", requestBodyMap);
// executeMap.put("execute", requestMap);
// reqMap.put(AdapterCanstants.BODY, executeMap);
// }else if("SVR2ECOMMActionWebService|035".equals(appId)) {
// Map<String,Object> requestHeaderMap = new HashMap<>();
// requestHeaderMap.put("Data_Typ", "2");
// requestHeaderMap.put("Serv_Typ", "1");
// requestHeaderMap.put("Serv_ID", "SVR_ECOMM_02_0002");
// requestHeaderMap.put("Chnl_Num", "037");
// requestHeaderMap.put("Termn_Num", "06");
// Map<String,Object> reqBusiInfoMap = new HashMap<>();
// Map<String,Object> requestBodyMap = new HashMap<>();
// Map<String,Object> requestMap = new HashMap<>();
// Map<String,Object> executeMap = new HashMap<>();
// reqBusiInfoMap.put("reqBusiInfo", bodyMap);
// requestBodyMap.put("requestBody", reqBusiInfoMap);
// requestBodyMap.put("requestHeader", requestHeaderMap);
// requestMap.put("request", requestBodyMap);
// executeMap.put("execute", requestMap);
// reqMap.put(AdapterCanstants.BODY, executeMap);
// }else{
// reqMap.put(AdapterCanstants.BODY, bodyMap);
// }
//
// String wsdl = "http://" + getIp() + ":" + getPort() + path;
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "this request path is {}", wsdl);
// String str = Xml2JsonUtil.json2Xml(JSONObject.fromObject(reqMap), systemId);
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "this request xml is {}", str);
// InputStream is = null;
// String result = null;
// try {
// HttpClient client = new HttpClient();
// PostMethod postMethod = new PostMethod(wsdl);
// // 设置连接超时
// client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
// // 设置读取时间超时
// client.getHttpConnectionManager().getParams().setSoTimeout(30000);
// // 然后把Soap请求数据添加到PostMethod中
// RequestEntity requestEntity = new StringRequestEntity(str, "text/xml", "UTF-8");
// // 设置请求头部，否则可能会报 “no SOAPAction header” 的错误
// // postMethod.setRequestHeader("SOAPAction","");
// // 设置请求体
// postMethod.setRequestEntity(requestEntity);
// int status = client.executeMethod(postMethod);
// // 打印请求状态码
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "this request status is {}", status);
// // 获取响应体输入流
// is = postMethod.getResponseBodyAsStream();
// result = IOUtils.toString(is,"UTF-8");
// } finally {
// is.close();
// }
// // 获取请求结果字符串
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "this response xml is {}", result);
// // 去除返回报文的命名空间
// result = Xml2JsonUtil.removeXmlNs(result);
// // xml转换为json
// String jsonStr = Xml2JsonUtil.xml2Json("Envelope", result);
// Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "this response json is {}", jsonStr);
// Map<String,Object> map = (Map<String,Object>) JSONObject.fromObject(jsonStr);
// Map<String,Object> resMap = new HashMap<>();
// resMap.put(AdapterCanstants.HEADER, map.get("Header"));
// resMap.put(AdapterCanstants.BODY, map.get("Body"));
// jsonStr = JSONObject.fromObject(resMap).toString();
// return convertToStandardResponse(jsonStr,"esb_commons_header_receive");
// }
//
//
// /**
// * 转换网关的esb标准响应报文
// * @param jsonStr
// */
// public String convertToStandardResponse(String jsonStr,String beanName) {
// //网关报文map
// Map<String,Object> gateWayMap = new HashMap<>();
// MessageDefine messageDefine = (MessageDefine) SpringContextsUtil.getBean(beanName);
// Map<String,Object> respMap = (Map<String,Object>) JSONObject.fromObject(jsonStr);
// Map<String,String> headerMap = (Map<String, String>) respMap.get("header");
// Map<String,String> soaHeaderMap = (Map<String, String>) JSONObject.fromObject(headerMap.get("soaHeader"));
//
// Map<String, DataElement> standardMap = messageDefine.getElementMap();
// for(String standardKey:standardMap.keySet()){
// if(soaHeaderMap.containsKey(standardKey)){
// soaHeaderMap.put(((MsgField)(standardMap.get(standardKey))).getRefName(),soaHeaderMap.get(standardKey));
// soaHeaderMap.remove(standardKey);
// }
// }
// gateWayMap.put(AdapterCanstants.HEADER,soaHeaderMap);
//
// if (soaHeaderMap.get("errorCode").equals("soaErr_000")) {
// Map<String, String> responseBodyMap = (Map<String, String>)respMap.get("body");
// if (null!=responseBodyMap.get("executeResponse")) {
//
// Map<String, String> executeResponseMap = (Map<String,
// String>)JSONObject.fromObject(responseBodyMap.get("executeResponse"));
// Map<String, String> responseMap = (Map<String, String>)JSONObject.fromObject(executeResponseMap.get("response"));
// Map<String, String> responseHeaderMap = (Map<String,
// String>)JSONObject.fromObject(responseMap.get("responseHeader"));
// MessageDefine bodyMessageDefine = (MessageDefine) SpringContextsUtil.getBean("esb_money_body_receive");
// Map<String, DataElement> standardBodyMap = bodyMessageDefine.getElementMap();
// for(String standardKey:standardBodyMap.keySet()){
// if(responseHeaderMap.containsKey(standardKey)){
// responseHeaderMap.put(((MsgField)(standardBodyMap.get(standardKey))).getRefName(),responseHeaderMap.get(standardKey));
// responseHeaderMap.remove(standardKey);
// }
// }
// responseHeaderMap.remove("bizDetail");
// gateWayMap.put(AdapterCanstants.BODY, responseHeaderMap);
// }else {
// Map<String, String> executeResponseMap = (Map<String,
// String>)JSONObject.fromObject(responseBodyMap.get("invokeResponse"));
//// Map<String, String> responseMap = (Map<String, String>)JSONObject.fromObject(executeResponseMap.get("return"));
// Map<String, String> responseHeaderMap = (Map<String, String>)JSONObject.fromObject(executeResponseMap.get("return"));
//// MessageDefine bodyMessageDefine = (MessageDefine) SpringContextsUtil.getBean("esb_msg_body_receive");
//// Map<String, DataElement> standardBodyMap = bodyMessageDefine.getElementMap();
//// for(String standardKey:standardBodyMap.keySet()){
//// if(responseHeaderMap.containsKey(standardKey)){
//// responseHeaderMap.put(((MsgField)(standardBodyMap.get(standardKey))).getRefName(),responseHeaderMap.get(standardKey));
//// responseHeaderMap.remove(standardKey);
//// }
//// }
//// responseHeaderMap.remove("bizDetail");
// gateWayMap.put(AdapterCanstants.BODY, responseHeaderMap);
// }
//
// }else {
// HashMap<String, String> map = new HashMap<String, String>();
// map.put("respCode", ErrorEnum.success.code());
// map.put("respMsg", "");
// gateWayMap.put(AdapterCanstants.BODY, map);
// }
// return JSONObject.fromObject(gateWayMap).toString();
// }
//
//
// @Override
// public void initConnector() {
//
// }
//
// @Override
// public String getIp() {
// return super.getIp();
// }
//
// @Override
// public void setIp(String ip) {
// super.setIp(ip);
// }
//
// @Override
// public String getPort() {
// return super.getPort();
// }
//
// @Override
// public void setPort(String port) {
// super.setPort(port);
// }
//
// @Override
// public String getUsname() {
// return super.getUsname();
// }
//
// @Override
// public void setUsname(String usname) {
// super.setUsname(usname);
// }
//
// @Override
// public String getPasswd() {
// return super.getPasswd();
// }
//
// @Override
// public void setPasswd(String passwd) {
// super.setPasswd(passwd);
// }
//
// @Override
// public IFormatter getRequestFormatter() {
// return super.getRequestFormatter();
// }
//
// @Override
// public void setRequestFormatter(IFormatter requestFormatter) {
// super.setRequestFormatter(requestFormatter);
// }
//
// @Override
// public IFormatter getResponseFormatter() {
// return super.getResponseFormatter();
// }
//
// @Override
// public void setResponseFormatter(IFormatter responseFormatter) {
// super.setResponseFormatter(responseFormatter);
// }
// }
